#pragma once

#include "sysmonitor/cpu_tools.hpp"
#include "sysmonitor/memory_tools.hpp"
#include "sysmonitor/file_system.hpp"
#include "sysmonitor/network_tools.hpp"
#include <functional>
#include <thread>
#include <atomic>
#include <chrono>
#include <map>

namespace sysmonitor {

// 系统资源综合信息
struct SystemResources {
    cpu::CpuUsage cpuUsage;
    memory::SystemMemoryInfo memoryInfo;
    std::vector<filesystem::DiskPartition> diskPartitions;
    std::vector<network::NetworkTraffic> networkTraffic;
    std::chrono::system_clock::time_point timestamp;
};

// 监控配置
struct MonitorConfig {
    std::chrono::milliseconds updateInterval = std::chrono::milliseconds(1000); // 默认1秒更新一次
    bool monitorCPU = true;
    bool monitorMemory = true;
    bool monitorDisk = true;
    bool monitorNetwork = true;
};

// 资源监控器
class ResourcesMonitor {

public:
    // 构造函数，接受配置和回调函数
    explicit ResourcesMonitor(
        const MonitorConfig& config = MonitorConfig(),
        std::function<void(const SystemResources&)> callback = nullptr
    );
  
    // 析构函数
    ~ResourcesMonitor();
  
    // 设置更新回调函数
    void setUpdateCallback(std::function<void(const SystemResources&)> callback);
  
    // 启动监控
    void startMonitoring();
  
    // 停止监控
    void stopMonitoring();
  
    // 获取CPU信息
    const cpu::CpuInfo& getCpuInfo() const;
  
    // 立即获取一次系统资源信息
    SystemResources getCurrentResources();

    // 是否正在运行
    bool isRunning() const;

private:
    MonitorConfig m_config;
    std::thread m_monitoringThread;
    std::atomic<bool> m_isRunning{false};
    std::atomic<bool> m_shouldStop{false};
  
    cpu::CpuInfo m_cpuInfo;
    std::vector<cpu::CpuTime> m_previousCpuTimes;
    network::NetworkMonitor m_networkMonitor;
  
    // 回调函数
    std::function<void(const SystemResources&)> m_updateCallback;
  
    // 监控循环
    void monitoringLoop();
};  


} // namespace sysmonitor
